package com.lazycoffee.lcloancalculator.components

import com.tencent.kuikly.core.base.Color
import com.tencent.kuikly.core.base.ComposeAttr
import com.tencent.kuikly.core.base.ComposeEvent
import com.tencent.kuikly.core.base.ComposeView
import com.tencent.kuikly.core.base.ViewBuilder
import com.tencent.kuikly.core.base.ViewContainer
import com.tencent.kuikly.core.views.compose.Button

enum class ButtonType {
    PRIMARY, SECONDARY
}

val ButtonType.backgroundColor: Color
    get() = when (this) {
        ButtonType.PRIMARY -> Color(150, 128, 114, 1F)
        ButtonType.SECONDARY -> Color(255, 255, 255, 1F)
    }

val ButtonType.textColor: Color
    get() = when (this) {
        ButtonType.PRIMARY -> Color(255, 255, 255, 1F)
        ButtonType.SECONDARY -> Color(150, 128, 114, 1F)
    }

class ButtonAttr : ComposeAttr() {
    var text = ""
    var type = ButtonType.PRIMARY
}

internal class ButtonEvent : ComposeEvent() {
    var clickHandler: (() -> Unit)? = null

    fun buttonClick(handler: () -> Unit) {
        clickHandler = handler
    }
}

internal class FormButtonView : ComposeView<ButtonAttr, ButtonEvent>() {
    override fun createAttr(): ButtonAttr {
        return ButtonAttr()
    }

    override fun createEvent(): ButtonEvent {
        return ButtonEvent()
    }

    override fun body(): ViewBuilder {
        val ctx = this
        return {
            Button {
                attr {
                    backgroundColor(ctx.attr.type.backgroundColor)
                    borderRadius(4F)
                    paddingTop(13F)
                    paddingBottom(13F)
                    titleAttr {
                        text(ctx.attr.text)
                        fontSize(18F)
                        color(ctx.attr.type.textColor)
                    }
                }
                event {
                    click {
                        ctx.event.clickHandler?.invoke()
                    }
                }
            }
        }
    }
}

internal fun ViewContainer<*, *>.FormButton(init: FormButtonView.() -> Unit) {
    addChild(FormButtonView(), init)
}